package com.xiaoming.freshmenapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.ClassmateDetailBean;
import com.xiaoming.freshmenapp.bean.MyBean;

import org.json.JSONException;
import org.json.JSONObject;

public class ClassmateDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView home_back;
    private TextView home_title;
    private LinearLayout home_layout;
    private TextView cctt_tv_bianhao;
    private TextView cctt_tv_name;
    private TextView cctt_tv_class;
    private TextView cctt_tv_plone;
    private TextView cctt_tv_emli;
    private Button cctt_bt_back;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmate_detail);
        initView();
        initData();

    }

    private void initData() {
        id = getIntent().getIntExtra("id",0);
        Log.e("TAG", "initData: "+ id);
        getData();
    }

    private void getData() {

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.2.64.83:8080/app4/classmate/findone";
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: "+jsonObject.toString());
                Gson gson = new Gson();
                ClassmateDetailBean myBean = gson.fromJson(jsonObject.toString(), ClassmateDetailBean.class);
                cctt_tv_bianhao.setText("编号        "+myBean.getData().getId());
                cctt_tv_name.setText("姓名        "+myBean.getData().getName());
                cctt_tv_class.setText("班级        "+myBean.getData().getClassname());
                cctt_tv_plone.setText("电话        "+myBean.getData().getTelephone());
                cctt_tv_emli.setText("邮箱        "+myBean.getData().getEmail());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private void initView() {
        home_back = (ImageView) findViewById(R.id.home_back);
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        home_title = (TextView) findViewById(R.id.home_title);
        home_title.setText("同学详情");
        home_layout = (LinearLayout) findViewById(R.id.home_layout);
        home_layout.setBackground(getResources().getDrawable(R.color.blu));

        cctt_tv_bianhao = (TextView) findViewById(R.id.cctt_tv_bianhao);
        cctt_tv_name = (TextView) findViewById(R.id.cctt_tv_name);
        cctt_tv_class = (TextView) findViewById(R.id.cctt_tv_class);
        cctt_tv_plone = (TextView) findViewById(R.id.cctt_tv_plone);
        cctt_tv_emli = (TextView) findViewById(R.id.cctt_tv_emli);
        cctt_bt_back = (Button) findViewById(R.id.cctt_bt_back);

        cctt_bt_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cctt_bt_back:
                finish();
                break;
        }
    }
}